package datos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import conexion.Conexion;

public class AccionesLinea {
	
	public static void addLinea (Linea l){

		try {
			Statement s = Conexion.conectar().createStatement();
			s.executeUpdate("INSERT INTO linea VALUES('"+l.getIdLinea()+"','"+l.getNombre()+"','"+l.getCategoria().getIdCategoria()+"')");
			
			s.close();			
			Conexion.desconectar();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static List<Linea> getAll(){
		List<Linea> lineas = new ArrayList<Linea>();
		Categoria c = null;
		try {
			new Conexion();
			Statement s = Conexion.conectar().createStatement();
			ResultSet rs = s.executeQuery("SELECT * FROM linea");
			
			while(rs.next()){
				String nombreLinea = rs.getString("nombreLinea");
				int idCategoria = rs.getInt("idCategoria");
				int idLinea = rs.getInt("idLinea");
				
				Statement s1 = Conexion.conectar().createStatement();
				ResultSet rs1 = s1.executeQuery("select * from categoria where idCategoria = '"+idCategoria+"'");
				while (rs1.next()) {
					c = new Categoria(rs1.getString("nombreCategoria"));
					c.setIdCategoria(rs1.getInt("idCategoria"));
				}
				
								
				Linea l = new Linea(nombreLinea, c);
				l.setIdLinea(idLinea);
				lineas.add(l);
				
				s1.close();
				
			}
			
			s.close();			
			Conexion.desconectar();
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lineas;
		
	}

	public static Linea getLineaById(int id) {
		Linea linea = null;
		Categoria cat = null;
		try {
			Statement s = Conexion.conectar().createStatement();
			ResultSet rs = s.executeQuery("select * from linea where idLinea='"+id+"'");
			while(rs.next()){
				//int idLinea = rs.getInt("idLinea");
				int idCategoria = rs.getInt("idCategoria");
				String nombre = rs.getString("nombreLinea");
				
				Statement s1 = Conexion.conectar().createStatement();
				ResultSet rs1 = s1.executeQuery("select * from categoria where idCategoria = '"+idCategoria+"'");
					while (rs1.next()){
						//System.out.println("entro");
							
							cat = new Categoria(rs1.getString("nombreCategoria")); 
							cat.setIdCategoria(idCategoria);
						//System.out.println("paso");
					}
				
				linea = new Linea(nombre, cat);
				linea.setIdLinea(id);
				//s1.close();
				
			}
			//s.close();
			//Conexion.desconectar();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		return linea;
	}
	
	public static boolean existeNombreLinea(Linea l){
		String nombre=l.getNombre();
		try {
			Statement stm=Conexion.conectar().createStatement();
			ResultSet rs=stm.executeQuery("select * from linea where nombreLinea='"+nombre+"'");
			if(rs.next()){
				return true;
			}
			stm.close();
			Conexion.desconectar();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}
	
	public static int getLineaByNombre(String nombreLinea) {
		Linea linea = null;
		int idLinea=0;
		try {
			Statement s = Conexion.conectar().createStatement();
			ResultSet rs = s.executeQuery("select * from linea where nombreLinea='"+nombreLinea+"'");
			while(rs.next()){
				int idCategoria = rs.getInt("idCategoria");
				
				Categoria categoria=AccionesCategoria.getCategoriaById(idCategoria);
				String nombre = rs.getString("nombreLinea");
				linea = new Linea(nombre, categoria);
				idLinea=rs.getInt("idLinea");
			}
			//s.close();
			//Conexion.desconectar();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		return idLinea;
	}
	
	public static boolean existeLinea(Linea l){
		String nombreLinea=l.getNombre();
		int idCategoria=l.getCategoria().getIdCategoria();
		try{
			Statement stm=Conexion.conectar().createStatement();
			ResultSet rs=stm.executeQuery("select * from linea where nombreLinea='"+nombreLinea+"' and idCategoria="+idCategoria);
			if(rs.next()){
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	}
